import { createRouter, createWebHashHistory } from 'vue-router';
import type { NavigationGuardNext, RouteLocationNormalized } from 'vue-router';
import { staticroutes } from './routes';

const router = createRouter({
  history: createWebHashHistory(),
  routes: staticroutes,
  scrollBehavior() {
    // 滚动行为
    return {
      left: 0,
      top: 0,
    };
  },
});

// 添加动态路由的函数
// import { useAuthStore } from '@/store/modules/auth'
// export function addDynamicRoutes() {
//   const authStore = useAuthStore();
//   const layoutRoute = staticroutes.find(route => route.path === '/layout');
//   if (layoutRoute && layoutRoute.children) {
//     layoutRoute.children.push(...authStore.authMenuList);
//     // 重新设置路由
//     router.addRoute(layoutRoute);
//   }
// }

//路由鉴权
import nprogress from 'nprogress';
import 'nprogress/nprogress.css';

// 隐藏右上角圆圈
nprogress.configure({
  showSpinner: false,
});

//全局前置守卫
router.beforeEach(
  async (
    to: RouteLocationNormalized,
    from: RouteLocationNormalized,
    next: NavigationGuardNext,
  ) => {
    // const authStore = useAuthStore();
    nprogress.start();
    // const requiresAuth = to.meta.requiresAuth;
    // const token = localStorage.getItem('user-access-token');
    // if (!token) {
    //   next('/login');
    // } else {
    //   next();
    // }
    next();
  },
);
// 全局后置守卫
router.beforeEach(() => {
  nprogress.done();
});

export default router;
